﻿using blog.Dao;
using blog.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using System.Data;
using System.Data.SqlClient;

namespace blog.Controllers
{
    [Route("api/[action]")]
    [ApiController]
    public class CommentController : Controller
    {
        [HttpPost]
        public Result<int> AddComment(Comment comment)  
        {
            Result<int> res = new Result<int>();
            int count = 0;
            string sql = "insert into comment(contentId,userId,createTime,commentMd,userName,userAvatar) values(@contentId,@userId,@createTime,@commentMd,@userName,@userAvatar)";
            SqlParameter[] parameters =
            {
                new SqlParameter("@contentId",comment.ContentId),
                new SqlParameter("@userId",comment.UserId),
                new SqlParameter("@createTime",DateTime.Now),
                new SqlParameter("@commentMd",comment.CommentMd),
                new SqlParameter("@userName",comment.UserName),
                new SqlParameter("@userAvatar",comment.UserAvatar),
            };
            count = DBHelper.ExecuteNonQuery(sql, parameters);
            CommentPlus(comment.ContentId);
            res.Message = "新增成功";
            res.Code = 200;
            res.Data = count;
            return res;
        }

        [HttpDelete]
        public Result<int> DeleteComment(int id)
        {
            Result<int> res = new Result<int>();
            int count = 0;
            string sql = "delete from comment where id=@id";
            SqlParameter[] parameters =
            {
                new SqlParameter("@id",id),
            };
            count = DBHelper.ExecuteNonQuery(sql, parameters);
            res.Message = "删除成功";
            res.Code = 200;
            res.Data = count;
            return res;
        }

        [HttpGet]
        public Result<List<Comment>> GetCommentByContentId(string blogId)
        {
            Result<List<Comment>> res=new Result<List<Comment>>();
            List<Comment> comments= new List<Comment>();
            string sql = "select * from comment where contentId=@blogId";
            SqlParameter[] parameters =
            {
                new SqlParameter("@blogId",blogId),
            };
            DataSet ds = DBHelper.GetDataSet(sql, parameters);

            //4、获得DataSet对象中指定的表（学生表）
            DataTable dt = ds.Tables[0];

            //5、遍历表中的每一行，把每行中的各个字段的值依次存入学生对象，添加学生对象到学生列表
            Comment comment = null;
            foreach (DataRow dr in dt.Rows)
            {
                comment = new Comment();
                comment.Id = Convert.ToInt32(dr["id"]);
                comment.ContentId = Convert.ToInt32(dr["contentId"]);
                comment.UserId = Convert.ToInt32(dr["userId"]);
                comment.CreateTime = Convert.ToDateTime(dr["createTime"]);
                comment.CommentMd = dr["commentMd"].ToString();
                comment.UserName = dr["userName"].ToString();
                comment.UserAvatar = dr["userAvatar"].ToString();
                comments.Add(comment);
            }
            res.Code = 200;
            res.Message = "查询成功";
            res.Data = comments;
            return res;
        }
        [HttpGet]
        public void CommentPlus(int? id)
        {
            string sql = "update content set commentCount=commentCount+1 where id=@id";
            SqlParameter[] parameters =
            {
                new SqlParameter("@id",id),
            };
            DBHelper.ExecuteNonQuery(sql, parameters);
        }

        

    }
}
